package org.example;

import com.huaweicloud.sdk.config.v1.ConfigClient;
import com.huaweicloud.sdk.config.v1.model.CreateStoredQueryRequest;
import com.huaweicloud.sdk.config.v1.model.CreateStoredQueryResponse;
import com.huaweicloud.sdk.config.v1.model.QueryRunRequestBody;
import com.huaweicloud.sdk.config.v1.model.RunQueryRequest;
import com.huaweicloud.sdk.config.v1.model.RunQueryResponse;
import com.huaweicloud.sdk.config.v1.model.StoredQueryRequestBody;
import com.huaweicloud.sdk.config.v1.region.ConfigRegion;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;

public class Main {
    public static void main(String[] args) {
        String ak = "<your ak>";
        String sk = "<your sk>";
        String regionId = "<region id>";

        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        ICredential auth = new GlobalCredentials()
            .withAk(ak)
            .withSk(sk);

        ConfigClient client = ConfigClient.newBuilder()
            .withHttpConfig(config)
            .withCredential(auth)
            .withRegion(ConfigRegion.valueOf(regionId))
            .build();

        CreateStoredQueryRequest createRequest = new CreateStoredQueryRequest()
            .withBody(new StoredQueryRequestBody()
                .withName("config_test")
                .withDescription("description_test")
                .withExpression("select count(*) from resources"));

        RunQueryRequest runRequest = new RunQueryRequest()
            .withBody(new QueryRunRequestBody()
                .withExpression("select count(*) from resources"));

        try {
            CreateStoredQueryResponse createResponse = client.createStoredQuery(createRequest);
            System.out.println(createResponse.toString());
            RunQueryResponse runResponse = client.runQuery(runRequest);
            System.out.println(runResponse.toString());
        } catch (ConnectionException | RequestTimeoutException | ServiceResponseException e) {
            System.out.println(e);
        }
    }
}